package ibex35.services;

import ibex35.beans.Ibex35Company;
import ibex35.beans.Ibex35CompanyList;
import ibex35.interfaces.Ibex35Dao;

import java.io.IOException;

import architecture.beans.EmptyBean;

public class Ibex35GetCompaniesValues {

	private static Ibex35Dao ibex35Dao;

	public static Ibex35CompanyList serviceExecution(final EmptyBean emptyBean)
			throws IOException {
		final Ibex35CompanyList out = new Ibex35CompanyList();

		// TODO: We should do the petition to URL here
		// http://apify.heroku.com/api/ibex35.json

		// if(jsonReturn.isEmpty()){
		// new ServiceException("Could not read Ibex35 json");
		// }
		for (final Ibex35Company lIbex35Company : out.getIbex35CompanyList()) {

			isInfoToSave(lIbex35Company);

			if(!getAveragePriceLastYear(lIbex35Company)){
				//throw exception
			}

			if(getAveragePriceLastThreeMonths(lIbex35Company)){
				//throw exception
			}

			if(getAveragePriceLastMonth(lIbex35Company)){
				//throw exception
			}


			if(getPriceLastThreeDays(lIbex35Company)){
				//throw exception
			}


			// shouldWeBuy

			// shouldWeSell
		}
		return out;
	}

	private static boolean getPriceLastThreeDays(final Ibex35Company pIbex35Company) {
		// TODO Auto-generated method stub
		Boolean greaterThanLastYear = false;
		final Ibex35Company lIbex35Company = ibex35Dao.getAVGlastThreeDays(pIbex35Company);
		if (lIbex35Company.getPrecio() * Float.valueOf("0'80") > pIbex35Company
				.getPrecio()) {
			greaterThanLastYear = true;
		}
		return greaterThanLastYear;
	}

	private static boolean getAveragePriceLastMonth(final Ibex35Company pIbex35Company) {
		// TODO Auto-generated method stub
		Boolean greaterThanLastYear = false;
		final Ibex35Company lIbex35Company = ibex35Dao.getAVGlastMonth(pIbex35Company);
		if (lIbex35Company.getPrecio() * Float.valueOf("0'80") > pIbex35Company
				.getPrecio()) {
			greaterThanLastYear = true;
		}
		return greaterThanLastYear;
	}

	private static boolean getAveragePriceLastThreeMonths(
			final Ibex35Company pIbex35Company) {
		// TODO Auto-generated method stub
		Boolean greaterThanLastYear = false;
		final Ibex35Company lIbex35Company = ibex35Dao.getAVGlastThreeMonths(pIbex35Company);
		if (lIbex35Company.getPrecio() * Float.valueOf("0'80") > pIbex35Company
				.getPrecio()) {
			greaterThanLastYear = true;
		}
		return greaterThanLastYear;

	}

	private static boolean getAveragePriceLastYear(final Ibex35Company plIbex35Company) {
		// TODO Auto-generated method stub
		Boolean greaterThanLastYear = false;
		final Ibex35Company lIbex35Company = ibex35Dao.getAVGlastYear(plIbex35Company);
		if (lIbex35Company.getPrecio() * Float.valueOf("0'80") > plIbex35Company
				.getPrecio()) {
			greaterThanLastYear = true;
		}
		return greaterThanLastYear;
	}

	private static void isInfoToSave(final Ibex35Company pIbex35Company) {
		// TODO Auto-generated method stub
		if (pIbex35Company != null) {

			// is hour to save?
			// TODO cuando guardamos?
			if (System.currentTimeMillis() == Long.valueOf(80000000000L)) {

				ibex35Dao.save(pIbex35Company);
			}

		}
	}

	public static void main(final String[] args) throws IOException {
		final Ibex35CompanyList serviceExecution = serviceExecution(new EmptyBean());
		System.out.println(serviceExecution);
	}

}
